package com.weishenai.customerassistant.controller;


import com.weishenai.customerassistant.common.Result;
import com.weishenai.customerassistant.entity.PublishingTask;
import com.weishenai.customerassistant.service.PublishingTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "管理系统 任务管理")
@RequestMapping("/publishingTask")
public class PublishingTaskController extends BaseController {
    @Autowired
    private PublishingTaskService publishingTaskService;

    @PreAuthorize("hasAuthority('task:publish:list')")
    @ApiOperation("查询当前供电所所有任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "substation", value = "供电所名称"),
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每一页的数量"),
    })
    @GetMapping("/selectBySubstation/{substation}/{pageNum}/{pageSize}/{keyword}")
    public Object selectBySubstation(@PathVariable(name = "substation") String substation,
                                     @PathVariable(name = "pageNum") Integer pageNum,
                                     @PathVariable(name = "pageSize") Integer pageSize,
                                     @PathVariable(name = "keyword") String keyWord) {
        return Result.success(publishingTaskService.selectBySubstation(substation, pageNum, pageSize, keyWord));
    }

    @PreAuthorize("hasAnyAuthority('task:publish:add','task:publish:edit')")
    @ApiOperation("新建或者修改一个任务")
    @ApiImplicitParam(name = "publishingTask", value = "")
    @PostMapping("/addTask")
    public Object AddTask(@RequestBody PublishingTask publishingTask) {
        return Result.success(publishingTaskService.addOrUpdateTask(publishingTask));
    }

    @PreAuthorize("hasAuthority('task:publish:delete')")
    @ApiOperation("通过id 删除一个任务")
    @ApiImplicitParam(name = "id", value = "任务id")
    @PostMapping("/deleteById/{id}")
    public Object deleteById(@PathVariable(name = "id") Long id) {
        return Result.success(publishingTaskService.deleteById(id));
    }
}
